package com.zhangpeng520.common.interceptor;

import com.zhangpeng520.user.entity.User;
import org.apache.shiro.session.Session;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/11 0011.
 */
public class SessionInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest Request, HttpServletResponse Response, Object o) throws Exception {


       String url=Request.getRequestURL().toString();


       if(url.indexOf("login")>=0||url.indexOf("register")>=0)
       {
          return true;


       }

       else if(url.indexOf("404")>=0||url.indexOf("500")>=0)
       {
           return true;
       }

       else
       {
           User user=(User)Request.getSession().getAttribute("loginInfo");

           if(user!=null)
           {
               return true;
           }
           else
           {
               Request.getRequestDispatcher("/login").forward(Request,Response);
               return false;
           }

       }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
